package com.sina.shop.manage.controller.goods;

import com.sina.shop.core.exception.server.UpdateException;
import com.sina.shop.domain.goods.Goods;
import com.sina.shop.domain.goods.GoodsPhoto;
import com.sina.shop.manage.controller.storage.goods.SinaStorageGoodsPhotoManager;
import com.sina.shop.manage.service.goods.GoodsPhotoManageService;
import com.sina.shop.manage.storage.CloudStorageException;
import com.sina.shop.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2018-01-15-15:42
 */
@Controller
@RequestMapping("goodsPhotoManage")
public class GoodsPhotoManageController extends BaseController {

    @Autowired
    @Qualifier("goodsPhotoManageService")
    private GoodsPhotoManageService goodsPhotoManageService;

    @Autowired
    @Qualifier("sinaStorageGoodsPhotoManager")
    private SinaStorageGoodsPhotoManager sinaStorageGoodsPhotoManager;

    @RequestMapping(value = "getGoodsPhotoList", method = RequestMethod.GET)
    public ModelAndView getGoodsPhotoList(@Valid GoodsPhoto goodsPhoto) {
        LOG.info("goodsPhoto:{}", goodsPhoto);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(6);
        Long count[] = new Long[1];
        List<Map<String, Object>> permissionList = goodsPhotoManageService.getGoodsPhotoList(goodsPhoto, count);

        Map<String, Object> data = new HashMap<>(4);
        data.put("recordsTotal", count[0]);
        data.put("recordsFiltered", count[0]);
        data.put("data", permissionList);

        result.put(CODE, OK);
        result.put(DATA, data);
        mv.addObject(RESULT, result);

        return mv;
    }


    @RequestMapping(value = "deleteGoodsPhoto", method = RequestMethod.POST)
    public ModelAndView deleteGoodsPhoto(Long id) {
        LOG.info("id:{}", id);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);

        try {
            goodsPhotoManageService.deleteGoodsPhoto(id);
            GoodsPhoto goodsPhoto = goodsPhotoManageService.getGoodsPhotoById(id);
            sinaStorageGoodsPhotoManager.deleteGoodsPhoto(goodsPhoto);
            result.put(CODE, OK);
            result.put(MSG, "删除商品照片成功");
        } catch (UpdateException e) {
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        } catch (Exception e) {
            result.put(CODE, "101010");
            result.put(MSG, "云存储删除失败，请手动删除保持同步");
        }

        mv.addObject(RESULT, result);

        return mv;
    }


    @RequestMapping(value = "updateGoodsPhoto", method = RequestMethod.POST)
    public ModelAndView updateGoodsPhoto(@Valid GoodsPhoto goodsPhoto) {

        LOG.info("goodsPhoto:{}", goodsPhoto);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);
        try {
            goodsPhotoManageService.updateGoodsPhoto(goodsPhoto);
            result.put(CODE, OK);
            result.put(MSG, "更新商品照片成功");
        } catch (UpdateException e) {
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "saveGoodsPhoto", method = RequestMethod.POST)
    public ModelAndView saveGoodsPhoto(@Valid GoodsPhoto goodsPhoto) {

        LOG.info("goodsPhoto:{}", goodsPhoto);


        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);
        try {
            goodsPhotoManageService.saveGoodsPhoto(goodsPhoto);
            result.put(CODE, OK);
            result.put(MSG, "新增商品照片成功");
        } catch (UpdateException e) {
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);

        return mv;
    }


    @RequestMapping(value = "getGoodsPhotoListToHtml", method = RequestMethod.GET)
    private ModelAndView getGoodsPhotoListToHtml() {

        ModelAndView mv = new ModelAndView("goods/goodsPhotoList");

        return mv;
    }
}
